import sql_metadata
from  sql_metadata import sqlparse

sql = '''
select K.a, K.b, (select m from S where p = 1) as m
  from I,   --fefe
       J, /* testes  tetse */ K,
       (select H.b
          from (select G.c
                  from (select F.d from (select E.e from A, B, C, D, E where e.id=1), F),
                       G),
               H)
 where i1 in (select t2 from T where t1 = 1)
   and exists (select 1 from S where s1 = j1)
union all
select K.a, K.b, (select m from S where p = 1) as m
  from I,
       J,
       K,
       (select H.b
          from (select G.c
                  from (select F.d from (select E.e from A LEFT      
                   JOIN   B , C, D, E where a.id=2), F),
                       G),
               H)
 where i1 in (select t2 from T where t1 = 2)
   and exists (select 1 from W where w1 = k1)
 order by 1, 2;
'''
sql = sqlparse.format(sql, strip_comments=True)
#sql = sqlparse.split(sql)
sql = ' '.join(sql.split())


print(sql_metadata.get_query_tables(sql))
